﻿using System;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Data;
using YiSha.Data.Repository;
using YiSha.Entity.QixingManage;
using YiSha.Model.Param.QixingManage;

namespace YiSha.Service.QixingManage
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2024-11-05 22:04
    /// 描 述：服务类
    /// </summary>
    public class GoodsTypesService :  RepositoryFactory
    {
        private ProductVarietyVarietyTypeService productVarietyVarietyTypeService = new ProductVarietyVarietyTypeService();
        #region 获取数据
        public async Task<List<GoodsTypesEntity>> GetList(GoodsTypesListParam param)
        {
            var expression = ListFilter(param);
            var list = await this.BaseRepository().FindList(expression);
            return list.ToList();
        }

        public async Task<List<GoodsTypesEntity>> GetPageList(GoodsTypesListParam param, Pagination pagination)
        {
            var expression = ListFilter(param);
            var list= await this.BaseRepository().FindList(expression, pagination);
            return list.ToList();
        }

        public async Task<GoodsTypesEntity> GetEntity(long id)
        {
            return await this.BaseRepository().FindEntity<GoodsTypesEntity>(id);
        }
        #endregion

        #region 提交数据
        public async Task SaveForm(GoodsTypesEntity entity)
        {
            try
            {
                //await db.BeginTrans();

                if (entity.Id.IsNullOrZero())
                {
                    entity.Create();
                    await this.BaseRepository().Insert(entity);
                }
                else
                {

                    await this.BaseRepository().Update(entity);
                }

                //开启绑定品牌
                if (entity.IsBrandModel == 1)
                {
                    foreach (var item in entity.typeBrands)
                    {
                        item.typeid = entity.Id;

                        GoodsTypeBrand typeBrand = this.BaseRepository().FindEntity<GoodsTypeBrand>(t => t.typeid == entity.Id && t.brandname.Equals(item.brandname)).Result;

                        if (typeBrand == null)
                        {
                            item.Create();
                            await this.BaseRepository().Insert(item);
                        }
                        else
                        { 
                            item.Id = typeBrand.Id;
                            await this.BaseRepository().Update(item);
                        }
                    }
                }
                //await db.CommitTrans();
            }
            catch (Exception e)
            {
                //await db.RollbackTrans();
            }
        }

        public async Task DeleteForm(string ids)
        {
            long[] idArr = TextHelper.SplitToArray<long>(ids, ',');
            await this.BaseRepository().Delete<GoodsTypesEntity>(idArr);
        }
        #endregion

        #region 私有方法
        private Expression<Func<GoodsTypesEntity, bool>> ListFilter(GoodsTypesListParam param)
        {
            var expression = LinqExtensions.True<GoodsTypesEntity>();
            if (param != null)
            {
                if (!param.pvid.IsNullOrZero())
                {
                    var pvt = productVarietyVarietyTypeService.GetList(new ProductVarietyVarietyTypeListParam() { pvid = param.pvid }).Result.Select(t => t.goodstypeid).ToList();
                    expression = expression.And(t => pvt.Contains(t.Id));
                }
                if (!param.type_name.IsEmpty())
                {
                    expression = expression.And(t =>t.type_name.Contains(param.type_name));
                }
            }
            return expression;
        }
        #endregion
    }
}
